package cm.pmfile;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.pm.Measurements;
import com.pm.Pm_Main;
import com.pm.Pm_file;
import com.pm.Pmfileheader;
import com.pm.Sqlfield;
import com.util.ConnectionPool;
import com.util.Constant;
import com.util.NrmUtil;

public class GeneratePmFileImp {
	private List<Pm_file> pmfileList;
	private String neName = "aaa";
	private BufferedWriter buf;
	private StringBuffer strBuf; 
	
	public void initPmfiles() {
		Pm_Main.parsePmConfig();
		pmfileList = Pm_Main.getPmfileList();
	}
	
	public void generateXml(){
		for (int i = 0; i < pmfileList.size(); i++) {
			String xmlneName = pmfileList.get(i).getXmlnename();
			if (neName.trim().equals(xmlneName)) {
			} else {
				String name = generateXmlName(pmfileList.get(i).getPmheaders().get(0).getPmfileheaders(),xmlneName);
				strBuf = new StringBuffer();
				writeMeasurements(pmfileList.get(i).getMeasurements());
				System.out.println(name);
				openFileWriter(name,strBuf.toString());
				NrmUtil.CompressNrmFile(name);
//				client.upLoadFile(name, "/home/yanbing/aaa.xml");
			}
			closeWriteBuf();
			ConnectionPool.releaseDbResource();
		}
//		client.closeFtpClient();
	}
	
	public String generateXmlName(List<Pmfileheader> pmfileheader,String type){
		String name = null;
		for (int i = 0; i < pmfileheader.size(); i++) {
			if (pmfileheader.get(i).getHeadername().trim()
					.equals(Constant.fileNameAttr)) {
				name = pmfileheader.get(i).getValue();
				break;
			}
		}
		if (name != null) {
			return "D:\\smallCell\\file\\" + type + "\\" + name + "-"
					+ NrmUtil.formatCurrentDate() + ".xml";
		} else {
			return name;
		}
	
		
	}
	
	public  void writerFileHeader(List<Pmfileheader> pmfileheader){
		strBuf.append(Constant.startPmfileHeader);
		for(int i=0;i<pmfileheader.size();i++){
			strBuf.append("<" + pmfileheader.get(i).getHeadername() + ">");
			strBuf.append(pmfileheader.get(i).getValue());
			strBuf.append("</" + pmfileheader.get(i).getHeadername() + ">"
					+ "\n");
		}
		strBuf.append(Constant.endPmfileHeader);
	}
	
	public void writeMeasurements(List<Measurements> measurements){
		strBuf.append(Constant.startPmObjects);
		for(int i=0;i<measurements.size();i++){
			String obj_type = measurements.get(i).getObjType();
			strBuf.append(Constant.startobjType + obj_type
					+ Constant.endobjType);
			List<Sqlfield> sqlfields = measurements.get(i).getSqlfields();
			writeMeasurementsAttribues(sqlfields);
			String sql = measurements.get(i).getSqlelement();
			ResultSet rs = ConnectionPool.getSqlResult(sql);
			try {
				while (rs.next()) {
					writeMeasurementValue(rs, sqlfields);
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		strBuf.append(Constant.endPmObjects);
	}
	
	public void writeMeasurementsAttribues(List<Sqlfield> sqlfields){
		strBuf.append(Constant.startPmfields);
		for(int i=0;i<sqlfields.size();i++){
			strBuf.append("\t<N i=" + i + ">" + sqlfields.get(i).getAttrname()
					+ "</N>\n");
		}
		strBuf.append(Constant.endPmfields);
	}
	
	public void writeMeasurementValue(ResultSet rs, List<Sqlfield> sqlfields){
		strBuf.append(Constant.startPmfieldVal);
		for (int i = 0; i < sqlfields.size(); i++) {
			writeValue(rs, sqlfields.get(i).getType(), sqlfields.get(i)
					.getAttrname(), i + 1);
		}
		strBuf.append(Constant.endPmfieldVal);
	}
	
	public void writeValue(ResultSet rs, String type, String name, int i){
		try {
			if (type.equals("int")) {
				strBuf.append("\t<V i=" + i + ">" + rs.getInt(name) + "</V>\n");
			} else if (type.equals("date")) {
				strBuf.append("\t<V i=" + i + ">" + rs.getDate(name) + "</V>\n");
			} else {
				strBuf.append("\t<V i=" + i + ">" + rs.getString(name) + "</V>\n");
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}
	
	public void openFileWriter(String fileName,String content) {
		if (fileName != null) {
			try {
				File nrmfile = new File(fileName);
				buf = new BufferedWriter(new FileWriter(nrmfile));
				buf.write(content);
				buf.flush();
			} catch (Exception e) {
				// throw(new Exception(e));
			}
		} else {
			System.exit(1);
		}
	}
	
	public void closeWriteBuf() {
		try {
			if (buf != null) {
				buf.flush();
				buf.close();
			}
		} catch (Exception e) {
			// throw(new Exception(e));
		}
	}
	
	public static void main(String[] args){
		GeneratePmFileImp imp = new GeneratePmFileImp();
		imp.initPmfiles();
		imp.generateXml();
	}
}
